<?php
  // 定义变量
  $tireqty = $_POST['Tireqty'];
  $oilqty = $_POST['oilqty'];
  $sparkqty = $_POST['sparkqty'];
  $address = $_POST['address'];
  $find  = $_POST['find'];
  $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
  $date = date('H:i,jS F Y ');
?>
<html>
<head>
  <meta charset="utf-8">
  <title>Avon`s Auto parts- order Results</title>
</head>
  <body>
    <h1>Avon`s Auto parts</h1>
    <h2>订单结果
      <?php
        echo " <p> 订单时间 ".date('H:i,js F Y ')."</p>";

        echo "<p>你的订单如下：</p>";
        $totalqty = 0;
        $totalqty = $tireqty + $oilqty + $sparkqty;
       echo "订单小计：".$totalqty."<br />";

      if( $totalqty == 0 ){

        echo "<p style='background-color: red;'>"."在前一页面您没有订购任何产品</br>"."</p>";

      }else{

        if($tireqty>0){
          echo $tireqty.' 轮胎<br/>';
        };
        if($oilqty>0){
          echo $oilqty.' 桶石油 <br/>';
        };
        if($sparkqty>0){
          echo $sparkqty.' 火花塞 <br/>';
        };
        echo "小计 :".$totalqty."<br/>";
      }

      switch($find){
        case "a":
          echo "<p>普通用户</p>";
          break;
        case "b":
          echo "<p>电视上看到的</p>";
          break;
        case "c":
          echo "<p>手机上看到的</p>";
          break;
        case "d":
          echo "<p>别人说的</p>";
          break;
        default:
          echo "<p>我也不知道该用户是通过什么了解的</p>";
          break;
      }

      $totalamount = 0.00;

      define('TIREPRICE',100);
      define('OILPRICE',10);
      define('SPARKPRICE',4);

      $totalamount = $tireqty * TIREPRICE
                    +$oilqty * OILPRICE
                    +$sparkqty * SPARKPRICE;

      $totalamount = number_format($totalamount,2,',',' ');

      echo "<p>总价:￥".$totalamount."</p>";
      echo "<p>地址距离我们是：".$address."</p>";

      $outputstring = $date."\t".$tireqty." tires \t".$oilqty." oil\t"
                      .$sparkqty." spark plugs\t\$".$totalamount
                      ."\t". $address."\n";

      //打开文件
      $fp =  fopen("$DOCUMENT_ROOT/php/order/orders.txt",'ab');

      flock($fp,LOCK_EX);

      if(!$fp){
        echo "<p><strong> 你现在没有修改的权限，请稍等</strong></p></body></html>";
        exit;
      }

      fwrite($fp,$outputstring,strlen($outputstring));
      flock($fp, LOCK_UN);
      fclose($fp);

      echo "<p>写入成功</p>";

      ?>
    </h2>
  </body>
</html>